package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsStatisticalWeightMapper;
import com.jy.wms.pojo.WmsStatisticalWeight;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2018/5/8.
 */
@Service
public class WmsStatisticalWeightService extends BaseService<WmsStatisticalWeightMapper,WmsStatisticalWeight>{
    @Resource
    private WmsStatisticalWeightMapper wmsStatisticalWeightMapper;

    /**
     * 分页查询
     * @param entity 实体
     * */
    @BaseServiceDescribe(moduleName = "各省重量统计报表", btnName = "分页查询")
    public Page<WmsStatisticalWeight> baseSelectPage(WmsStatisticalWeight entity) throws Exception {
        DecimalFormat df = new DecimalFormat("#.00");
        Page<WmsStatisticalWeight> list=null;
        //各省汇总
        double hz=0;
        //总计
        double oneZj=0;
        double twoZj=0;
        double threeZj=0;
        double fourZj=0;
        double hzhj=0;
        //区间比
        double oneQjb=0;
        double twoQjb=0;
        double threeQjb=0;
        double fourQjb=0;
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            list=(Page<WmsStatisticalWeight>) dao.baseSelectPage(entity);//分页查询
            WmsStatisticalWeight statisticalWeight=new WmsStatisticalWeight();
            WmsStatisticalWeight weight=new WmsStatisticalWeight();
            for(WmsStatisticalWeight wmsStatisticalWeight:list){
                //查询0-1KG含1各省的运输单数量
                double one=dao.oneNumber(wmsStatisticalWeight.getProvince());
                wmsStatisticalWeight.setOneKg(one);
                //查询1-2KG含2各省的运输单数量
                double two=dao.twoNumber(wmsStatisticalWeight.getProvince());
                wmsStatisticalWeight.setTwoKg(two);
                //查询2-3KG含3各省的运输单数量
                double three=dao.threeNumber(wmsStatisticalWeight.getProvince());
                wmsStatisticalWeight.setThreeKg(three);
                //查询3以上各省的运输单数量
                double four=dao.fourNumber(wmsStatisticalWeight.getProvince());
                wmsStatisticalWeight.setFourKg(four);
                //各省汇总
                hz=one+two+three+four;
                wmsStatisticalWeight.setProvinceSummary(hz);
                //各区间合计
                oneZj+=wmsStatisticalWeight.getOneKg();
                twoZj+=wmsStatisticalWeight.getTwoKg();
                threeZj+=wmsStatisticalWeight.getThreeKg();
                fourZj+=wmsStatisticalWeight.getFourKg();
                hzhj+=wmsStatisticalWeight.getProvinceSummary();
            }
            statisticalWeight.setProvince("总计");
            statisticalWeight.setOneKg(oneZj);
            statisticalWeight.setTwoKg(twoZj);
            statisticalWeight.setThreeKg(threeZj);
            statisticalWeight.setFourKg(fourZj);
            statisticalWeight.setProvinceSummary(hzhj);
            weight.setProvince("重量区间占全区间比（%）");
            oneQjb= statisticalWeight.getOneKg()/statisticalWeight.getProvinceSummary();
            weight.setOneKg(Double.parseDouble(df.format(oneQjb*100)));
            twoQjb=statisticalWeight.getTwoKg()/statisticalWeight.getProvinceSummary();
            weight.setTwoKg(Double.parseDouble(df.format(twoQjb*100)));
            threeQjb=statisticalWeight.getThreeKg()/statisticalWeight.getProvinceSummary();
            weight.setThreeKg(Double.parseDouble(df.format(threeQjb*100)));
            fourQjb=statisticalWeight.getFourKg()/statisticalWeight.getProvinceSummary();
            weight.setFourKg(Double.parseDouble(df.format(fourQjb*100)));
            list.add(statisticalWeight);
            list.add(weight);
            return list;
        } catch (Exception e) {
            throw new ServiceException("各省重量统计报表分页查询失败", e);
        }
    }

    /**
     * 导出各省重量统计信息
     * @param entity
     * @return
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出各省重量报表")
    public List<WmsStatisticalWeight> exportStatisticalWeight(WmsStatisticalWeight entity){
        DecimalFormat df = new DecimalFormat("#.00");
        //各省汇总
        int hz=0;
        List<WmsStatisticalWeight> list=dao.exportStatisticalWeight(entity);
        for(WmsStatisticalWeight wmsStatisticalWeight:list){
            //查询0-1KG含1各省的运输单数量
            int one=dao.oneNumber(wmsStatisticalWeight.getProvince());
            wmsStatisticalWeight.setOneKg(one);
            //查询1-2KG含2各省的运输单数量
            int two=dao.twoNumber(wmsStatisticalWeight.getProvince());
            wmsStatisticalWeight.setTwoKg(two);
            //查询2-3KG含3各省的运输单数量
            int three=dao.threeNumber(wmsStatisticalWeight.getProvince());
            wmsStatisticalWeight.setThreeKg(three);
            //查询3以上各省的运输单数量
            int four=dao.fourNumber(wmsStatisticalWeight.getProvince());
            wmsStatisticalWeight.setFourKg(four);
            //各省汇总
            hz=one+two+three+four;
            wmsStatisticalWeight.setProvinceSummary(hz);
        }
        return list;
    }
}
